<?php
/**
 * lay lai toan bo gallery trong DB 
 * @return array($header, $rs, $pager);
 * @author anhmantk
 * @date Dec 31, 2011
 */
function getAllGalery($condition = array()) {
    $header = array(
        array('data' => 'Tiêu đề'),
        array('data' => 'Gallery', 'width' => '250'),
        array('data' => 'Ở trang', 'field' => 'page'),
        array('data' => 'Click', 'width' => '50'),
        array('data' => 'Thời gian (m/d/Y)'),
        array('data' => 'Vị trí', 'width' => '50', 'field' => 'position'),
        array('data' => 'Trạng thái', 'width' => '80'),
        array('data' => 'Sửa', 'width' => '30'),
        array('data' => 'Khóa', 'width' => '30')
    );

    //phan nay se tao bo loc tren form quan ly de filter theo cac tieu chi :)    
    $where = "";
    if ($condition['page']) {
    	$where .= ' AND `page` = ' . $condition['page'];
    }
    
    if ($condition['start'] && $condition['end']) {
    	$where .= ' AND `start` >= ' . $condition['start'] . ' AND `end` <= ' . $condition['end'];
    } else {
    	if ($condition['start']) {
    		$where .= ' AND `start` >= ' . $condition['start'];
    	}	
    	if ($condition['end']) {
    		$where .= ' AND `end` <= ' . $condition['end'];
    	}
    	
    }

	
    $query = "SELECT *, (SELECT COUNT(id) FROM {tracking_ads_gallery} WHERE gallery_id = t1.id) AS click FROM {ads_gallery} AS t1 WHERE 1 = 1 " . $where . tablesort_sql($header);
    $count_query = "SELECT COUNT(id) FROM {ads_gallery} WHERE 1 = 1 " . $where;
    
    $results = pager_query($query, GALLERY_PERPAGE, 0, $count_query);    
    while(($obj = db_fetch_object($results)) != null) {
        $rs[] = $obj;
    }

    $pager = theme('pager');
    return array($header, $rs, $pager);
}
/**
 * lay toan bo gallery 
 * @author anhmantk
 */
function getAllGalleryShow($page_index, $origent) {
    $query = "SELECT `page`, `position`, `title`, `file`, `type`, `link`, `uid`, `sid`, `rid`, `hid`, `start`, `end` FROM {ads_gallery} WHERE `status` = %d AND `page` = %d AND SUBSTRING(position + '',1,1) = %d ORDER BY `page` ASC";
    $results = db_query($query, array(GALLERY_STATUS_SHOW, $page_index, $origent));
    while(($obj = db_fetch_object($results)) != null) {
        $rs[] = $obj;
    }
    return $rs;
}

/**
 * lay lai thong tin gallery theo id
 * @param int $gallery_id
 * @return stdClass thong tin gallery
 * @author anhmantk
 * @date Dec 31, 2011
 */
function getGalleryById($gallery_id) {
    $query = "SELECT * FROM {ads_gallery} WHERE id = %d";
    return db_fetch_object(db_query($query, $gallery_id));
}


/**
 * them moi gallery
 * @param array $params
 *        array('page', 'title', 'file', 'type', 'link', 'uid', 'sid', 'start', 'end', 'status', 'position')
 * @return boolean
 * @author anhmantk
 * @date Dec 31, 2011
 */
function insertGallery($params = array()) {
    $query = "INSERT INTO {ads_gallery} (`page`, `title`, `file`, `type`, `link`, `uid`, `sid`, `start`, `end`, `status`, `position`) VALUES (%d, '%s', '%s', %d, '%s', %d, %d, %d, %d, %d, %d)";
    return db_query($query, $params);
}
/**
 * sua gallery
 * @param array $params
 *        array('page', 'title', 'file', 'type', 'link', 'start', 'end', 'status', 'position', 'id') 
 * @return boolean
 * @author anhmantk
 * @date Dec 31, 2011
 */
function editGallery($params = array()) {
    $query = "UPDATE {ads_gallery} SET `page` = %d, `title` = '%s', `file` = '%s', `type` = %d, `link` = '%s', `start` = %d, `end` = %d, `status` = %d, `position` = %d WHERE id = %d";
    return db_query($query, $params);
}

/**
 * lock or unlock gallery
 * @param int $status
 * @param int $gallery_id
 * @return boolean
 * @author anhmantk
 * @date Dec 31, 2011
 */
function lockUnlockGallery($status = 0 ,$gallery_id) {
    $query = "UPDATE {ads_gallery} SET status = %d WHERE id = %d";
    return db_query($query, array($status, $gallery_id));
}

/**
 * moi khi nguoi dung click vao banner quang cao 
 * se luu lai IP va de biet co bao lam click
 * @param array $params
 * @return boolean
 * @author anhmantk
 */
function trackingGallery($params = array()) {
    // kiem tra neu IP nay da click banner roi thi thoi
    // con chua click banner nay thi them thong tin client
    $check = "SELECT * FROM {tracking_ads_gallery} WHERE gallery_id = %d AND ip = '%s'";
    $result = db_query($query, array($params['gallery_id'], $params['ip']));
    if(db_fetch_object($result) == null) {
        $query = "INSERT INTO {tracking_ads_gallery} (gallery_id, uid, ip) VALUES (%d, '%s', '%s')";
        return db_query($query, $params);
    }   
    return true;
}